<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/6/28
 * Time: 1:47
 */

namespace app\home\model;


class Construction extends Base
{
    protected $insert = [
        'create_time' => NOW_TIME,
    ];

    /**
     * 获取详情数据及加载更多
     * @param $type 类型：1红色驿站 2职工之家 3青年团体
     * @param int $leng 加载长度
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($uid,$type,$leng = 0)
    {
        $map = [
            'type' => $type,
            'status' => 0
        ];
        $list = $this->where($map)->limit($leng,10)->order('create_time desc')->select();
        foreach ($list as $key => $value) {
            $value = $value->toArray();
            //用户信息处理
            $value['time'] = date('Y-m-d H:i',$value['create_time']);
            $message = $this->getUser($value['uid']);
            $value['username'] = $message['name'];
            $value['department_name'] = $message['department_name'];
            $value['header'] = $message['header'] ? $message['header'] : $message['avatar'] ;
            $path = [];
            //图片处理
            if ($value['images']){
                $pic = json_decode($value['images'],true);
                foreach ($pic as $item){
                    $path[] = get_cover($item);
                }
            }
            if($path) {
                foreach ($path as $v) {
                    $v['index_path'] = thumb($v['path']);
                    $v['preview_path'] = thumb($v['path'],1200,720);
                }
            }
            $value['img'] = $path;
            //获取评论
            $comment = new Comment();
            $value['comment'] = $comment->getComment(3,$value['id'],$uid);
            //点赞
            $likeModel = new Like();
            $like = $likeModel->getLike(3,$value['id'],$uid);
            $value['is_like'] = $like;
            $list[$key] = $value;
        }
        return $list;
    }

    public function getUser($uid)
    {
        $user = WechatUser::where(['userid' => $uid])->field('userid,name,header,avatar,department')->find();
        $department = WechatDepartment::where(['id' => $user['department']])->find();
        if($user) {
            $user['department_name'] = $department['name'];
        }
        return $user;
    }

}